package com.ysd.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class DateUtils {

	private static final DateFormat mmddFormat = new SimpleDateFormat("MM-dd");
	private static final DateFormat hhmmFormat = new SimpleDateFormat("HH:mm");

	/**
	 * 闁兼儳鍢茶ぐ锟�2濞戞搩浜濆Λ鈺呭嫉閻斿墎顓洪梻鍌氼嚟濞堟垵袙韫囨挶浜�
	 * 
	 * @param p_start
	 *            Start Date
	 * @param p_end
	 *            End Date
	 * @return Dates List
	 */
	public List<Date> getDates(Calendar p_start, Calendar p_end) {
		List<Date> result = new ArrayList<Date>();
		Calendar temp = p_start.getInstance();
		temp.add(Calendar.DAY_OF_YEAR, 1);
		while (temp.before(p_end)) {
			result.add(temp.getTime());
			temp.add(Calendar.DAY_OF_YEAR, 1);
		}
		return result;
	}

	/**
	 * 闁兼儳鍢茶ぐ鍢ring闁哄啨鍎插﹢锟犳儍閿燂拷 濞戞挸锕ｇ粩瀛樺緞閿燂拷 String 闁哄啨鍎插﹢锟�
	 * 
	 * @throws ParseException
	 */
	public static String getPreviousDay(String strdate) throws ParseException {
		String str = "";
		if (!StringUtil.isNull(strdate)) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();// 闁告帗绻傞～鎰板礌閺堢笢lendar
			calendar.setTime(formatter.parse(strdate));// 閺夌儐鍓欑�垫煡骞嬮幁鍧卨endar闁哄啨鍎卞鏄忋亹閵忕姷纭�
			calendar.add(Calendar.DATE, -1);// 閻庡湱鍋熼獮鍥礉閻樻彃娅ら弶鈺傚姉閻ｅ鏁嶇仦鑲╀紣濠碘�冲亰缁变即宕濋悩杈╊伇濠㈣翰鍏榓lendar.add(Calendar.DATE,1);闁告垵绻嬬粩瀛樺緞閵夌禈lendar.add(Calendar.DATE,-1);
			str = formatter.format(calendar.getTime());// 闁兼儳鍢茶ぐ鍥礉閻樻彃娅ら弶鈺傚姉閻ｅ宕ユ惔锝嗙暠闁哄啨鍎插﹢锟犵嵁閹増绁柛鏍ㄧ墬閸ㄦ瓔tring
		}
		return str;
	}
	

	/**
	 * 闁兼儳鍢茶ぐ鍢ring闁哄啨鍎插﹢锟犳儍閿燂拷 闁告挸閽炲璁规嫹 String 闁哄啨鍎插﹢锟�
	 * 
	 * @throws ParseException
	 */
	public static String getPNDay(String strdate,int days) throws ParseException {
		String str = "";
		if (!StringUtil.isNull(strdate)) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();// 闁告帗绻傞～鎰板礌閺堢笢lendar
			calendar.setTime(formatter.parse(strdate));// 閺夌儐鍓欑�垫煡骞嬮幁鍧卨endar闁哄啨鍎卞鏄忋亹閵忕姷纭�
			calendar.add(Calendar.DATE, days);// 閻庡湱鍋熼獮鍥礉閻樻彃娅ら弶鈺傚姉閻ｅ鏁嶇仦鑲╀紣濠碘�冲亰缁变即宕濋悩杈╊伇濠㈣翰鍏榓lendar.add(Calendar.DATE,1);闁告垵绻嬬粩瀛樺緞閵夌禈lendar.add(Calendar.DATE,-1);
			str = formatter.format(calendar.getTime());// 闁兼儳鍢茶ぐ鍥礉閻樻彃娅ら弶鈺傚姉閻ｅ宕ユ惔锝嗙暠闁哄啨鍎插﹢锟犵嵁閹増绁柛鏍ㄧ墬閸ㄦ瓔tring
		}
		return str;
	}
	/**
	 * 闁兼儳鍢茶ぐ鍢ring闁哄啫鐖煎Λ鍧楁儍閿燂拷 闁告艾绐夌紒澶嬪笧濞堟叀tring 闁哄啨鍎插﹢锟�
	 * 
	 * @throws ParseException
	 */
	public static String getPNSecond(String strdatetime,int seconds) throws ParseException {
		String str = "";
		if (!StringUtil.isNull(strdatetime)) {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar calendar = Calendar.getInstance();// 闁告帗绻傞～鎰板礌閺堢笢lendar
			calendar.setTime(formatter.parse(strdatetime));// 閺夌儐鍓欑�垫煡骞嬮幁鍧卨endar闁哄啨鍎卞鏄忋亹閵忕姷纭�
			calendar.add(Calendar.SECOND, seconds);// 閻庡湱鍋熼獮鍥礉閻樻彃娅ら弶鈺傚姉閻ｅ鏁嶇仦鑲╀紣濠碘�冲亰缁变即宕濋悩杈╊伇缂佸濡絘lendar.add(Calendar.SECOND,1);闁告垵绻嬬粩瀵哥矓閹跺硽lendar.add(Calendar.SECOND,-1);
			str = formatter.format(calendar.getTime());// 闁兼儳鍢茶ぐ鍥礉閻樻彃娅ら弶鈺傚姉閻ｅ宕ユ惔锝嗙暠闁哄啨鍎插﹢锟犵嵁閹増绁柛鏍ㄧ墬閸ㄦ瓔tring
		}
		return str;
	}
	
	/**
	 * 閺夆晜鐟﹂悧閬嶅礃濞嗘劕顤呴柤鍏呯矙娴尖晠宕楀鍜佸殸閻犵儑绱曞▓鎴炵缂佹ê澹堥柛鎺撶☉缂傛捇鏁嶅畝鍐╁涧闁告帗澹嗛悾婵囩瑹閸喖瀚夐柤鍐诧拷鐔肺濋柣顏冪閸炲锟芥稒顭囬埞鏍⒒閿燂拷
	 * 
	 * @author XuGuo
	 * @since 2009-07-23
	 * @param date
	 * @return
	 */
	public static String formatMD(Date date) {
		return date == null ? "" : mmddFormat.format(date);
	}

	public static String formatHM(Date date) {
		return date == null ? "" : hhmmFormat.format(date);
	}

	public static String formatYMDHMS(String datestr) {

		return datestr == null ? "" : formatDateTime(StrToDate2(datestr, null),
				"yyyyMMddHHmmss");
	}

	public static String formatYMD(String datestr) {
		return datestr == null ? "" : formatDateTime(StrToDate(datestr, null),
				"yyyy-MM-dd");
	}

	public static String formatDateTime(Date date, String format) {
		if (date == null)
			return "";
		if (format == null)
			return date.toString();
		DateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	public static String formatY0M0D(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyyMMdd");
	}

	public static String formatMMHHSS(Date date) {
		return date == null ? "" : formatDateTime(date, "HHmmss");
	}

	public static String formatYMD(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyy-MM-dd");
	}

	public static String formatYMDHM(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyy-MM-dd HH:mm");
	}

	public static String formatDateTimeByDate(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static String formatDateTimeByDate_f2(Date date) {
		return date == null ? "" : formatDateTime(date, "yyyyMMddHHmmss");
	}

	public static boolean showNew(Date time) {
		if (time == null)
			return false;
		return DateUtils.addDays(time, 3).compareTo(new Date()) >= 0;
	}

	public static Date addDays(Date srcDate, int addDays) {
		return getNextDayCurrDay(srcDate, addDays);
	}

	public static Date addMinutes(Date srcDate, int minutes) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(srcDate);
		cal.add(Calendar.MINUTE, minutes);
		return cal.getTime();
	}

	public static Date getNextDayCurrDay(Date currDate, int i) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(currDate);
		gc.add(GregorianCalendar.DATE, i);
		return gc.getTime();
	}

	public static int getCurrDay() {
		Calendar now = Calendar.getInstance();
		return now.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 闁哄啨鍎插﹢鈥承掗弮鍥╃獩濠㈠爢鍐瘓 isBig=true:date1<date2
	 */
	public static boolean isBigDateForDate(Date date1, Date date2) {
		boolean isBig = false;
		isBig = date1.before(date2);
		return isBig;
	}

	/**
	 * 闁哄啨鍎插﹢鈥承掗弮鍥╃獩濠㈠爢鍐瘓 isBig=true:date1<date2
	 */
	public static boolean isBigDateForStr(String date1, String date2) {
		boolean isBig = false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date d1 = sdf.parse(date1);
			Date d2 = sdf.parse(date2);
			isBig = d1.before(d2);
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}

		return isBig;
	}

	/**
	 * 閻庢稒顨堥浣圭▔閼艰埖绁柛鏍ㄧ墧鐠愮喖寮妷锔藉焸
	 * 
	 * @param str
	 *            閻炴凹鍋夊ù鍡涘礌閺嶎偅鐣遍悗娑欘殘椤戜焦绋夐敓锟�
	 * @param format
	 *            閺夌儐鍓欑�垫煡寮介悡搴ｇ
	 * @return 閺夆晜鏌ㄥú鏍籍閵夛附鍩�
	 * @throws ParseException
	 * @author sys53
	 * @serialData 2007-11-03
	 */
	public static Date StrToDate(String str, String format) {
		try {
			if (StringUtil.isNull(format))
				format = "yyyy-MM-dd";
			java.text.SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(str);
		} catch (ParseException pe) {
			return null;
		}
	}

	/**
	 * 閻庢稒顨堥浣圭▔閼艰埖绁柛鏍ㄧ墧鐠愮喖寮妷锔藉焸2
	 * 
	 * @param str
	 *            閻炴凹鍋夊ù鍡涘礌閺嶎偅鐣遍悗娑欘殘椤戜焦绋夐敓锟�
	 * @param format
	 *            閺夌儐鍓欑�垫煡寮介悡搴ｇ
	 * @return 閺夆晜鏌ㄥú鏍籍閵夛附鍩�
	 * @throws ParseException
	 * @author sys53
	 * @serialData 2007-11-03
	 */
	public static Date StrToDate2(String str, String format) {
		try {
			if (StringUtil.isNull(format))
				format = "yyyy-MM-dd HH:mm:ss";
			java.text.SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(str);
		} catch (ParseException pe) {
			return null;
		}
	}

	/**
	 * 閻庢稒顨堥浣圭▔閼艰埖绁柛鏍ㄧ墧鐠愮喖寮妷锔藉焸,濮掓稒顭堥濠氬冀閻撳海纭�濞戞搫鎷�:yyyy-MM-dd
	 * 
	 * @param str
	 * @return
	 * @throws ParseException
	 */
	public static Date StrToDate(String str) {
		return StrToDate(str, "yyyy-MM-dd");
	}

	/**
	 * 闁告帇鍊栭弻鍥蓟閹邦兙浜柡鍕靛灠閹線宕烽妸锔惧帣濞戞搩浜濆Σ锕傚嫉閻斿憡顦ч梻鍌涙綑閸烇拷 婵絾鏌ㄩ々锟�"2009-05-10" 闁哄嫷鍨伴幆渚�宕烽敓锟�"1,2,4,5"闁哄嫮鍠愬﹢锟犲礃閿燂拷
	 * 
	 * @param strDate
	 * @param week
	 * @return
	 */
	public static boolean isExistInWeek(String strDate, String week) {
		Date date = StrToDate(strDate);
		int days = dayOfWeek(date);
		if (week.indexOf(String.valueOf(days)) >= 0) {
			return true;
		}
		return false;
	}

	// 闁告帇鍊栭弻鍥籍閵夛附鍩傚☉鎾跺劋濡诧箓寮甸悢宄版,1濞戞挾鍎ゅΣ锕傚嫉閻斿墎顏�,6濞戞挾鍎ゅΣ锕傚嫉閻斿嘲褰�,7濞戞挾鍎ゅΣ锕傚嫉閻旀灚浜柨娑樺缁跺嘲顫㈤妶鍥潶闁圭尨鎷�
	public static int dayOfWeek(Date date) {
		// 濡絾鐗曢崢娑氾拷瑙勭煯缁犵喐绋夐敓鑺ョ▔閻氭瓫lendar闁挎稑鑻换锟藉銈堫唺婵炲洭鎮介埡顤玹Instance()閺夆晜绋栭、鎴狅拷鍦仒缁躲儵宕犻敓锟�
		Calendar aCalendar = Calendar.getInstance();
		// 闂佹彃鐭傚浼存煂鎼粹�宠濞寸姰鍎冲ú鍧楀箳閵夛箑绲婚柛蹇ｆ珨ate缂侇偉顕ч悗锟�
		aCalendar.setTime(date);
		// 閻犱緤绱曢悾璇差潰閵堝棙锛夐柡鍫㈠枑濡插憡绋夐敓浠嬪川閵娿倛鍘柣銊ュ閹姐垺绋夐敓鑺ュ緞閿燂拷
		int x = aCalendar.get(Calendar.DAY_OF_WEEK);
		if (x == 1)
			x = 7;
		else
			x = x - 1;
		return x;
	}

	public static void main(String[] args) {
		// System.out.println(isExistInWeek("2009-06-11","1,2,5,7"));
		// System.out.println(getParsedDate("06MAY09"));
		// System.out.println(getDiffDays(formatYMD(getNextDayCurrDay(new
		// Date(),10)),formatYMD(new Date())));
		//System.out.println(formatDateTime(getLastDate(new Date()),"yyyy-MM"));
	}

	/**
	 * 閺夌儐鍓氬畷鑼拷娑欘殘椤戜焦绋夐崣澶嬶級闁哄牏鍠撶悮顐﹀垂鐎ｂ晞绀� "yyyy-MM-dd" 缂侇偉顕ч悗锟�
	 * 
	 * @param date
	 *            06MAY09
	 * @return
	 * 
	 */
	public static String getParsedDate(String strDate) {
		String[] monIntArray = { "01", "02", "03", "04", "05", "06", "07",
				"08", "09", "10", "11", "12" };
		String[] monStrArray = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
				"JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
		String year = "20" + strDate.substring(5, 7);
		String month = strDate.substring(2, 5);
		String day = strDate.substring(0, 2);
		for (int i = 0; i < monStrArray.length; i++) {
			if (monStrArray[i].equalsIgnoreCase(month)) {
				month = monIntArray[i];
				break;
			}
		}
		return year + "-" + month + "-" + day;
	}

	public static long getNumOfDays(String date1, String date2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date d1;
		try {
			// if is true ,date1>date2
			if (isBigDateForStr(date1, date2)) {
				d1 = df.parse(date1);
				Date d2 = df.parse(date2);
				long diff = Math.abs(d2.getTime() - d1.getTime());
				return (long) (-diff / (1000 * 60 * 60 * 24));
			} else {
				d1 = df.parse(date1);
				Date d2 = df.parse(date2);
				long diff = Math.abs(d2.getTime() - d1.getTime());
				return (long) (diff / (1000 * 60 * 60 * 24));
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static long getDiffDays(String date1, String date2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date d1;
		long diff = 0;
		try {
			d1 = df.parse(date1);
			Date d2 = df.parse(date2);
			long diff_1 = d2.getTime() - d1.getTime();
			if (diff_1 >= 0) {
				diff = Math.abs(diff_1);
				return (long) (diff / (1000 * 60 * 60 * 24));
			} else {
				return (long) (diff_1 / (1000 * 60 * 60 * 24));
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static long getNumOfDays(Date date1, Date date2) {
		long diff = Math.abs(date2.getTime() - date1.getTime());
		return (long) (diff / (1000 * 60 * 60 * 24));
	}

	/**
	 * 闁哄偆鍘奸崹鑺ョ▔閵堝嫰鍤嬮柡鍐﹀劜濠�鈩冪▕鐎ｎ喗锛熼柡鍐硾濡﹪寮伴姘剨闁革讣鎷�5闁告帒妫濋幐鎾寸閵夈倗鐟�
	 * 
	 * @param d1
	 *            闁靛棴鎷烽柡鍐﹀劜濠�锟�1
	 * @param d2
	 *            闁哄啨鍎插﹢锟�2
	 * @return 閺夆晜鏌ㄥú鏉ue濞戞挶鍊撻柌婊堝籍閵夛附鍩傚☉鏂款儔濡潡鎯勭粙鎸庘枙5闁告帒妫濋幐鎾寸閵夈倗鐟愰柨娑樼暫alse闁烩晝顭堝Ο濠囧础娴ｇ鐎婚梺鐣屽枍娴滄帡宕橀敓锟�.
	 */
	public static boolean compare(Date d1, Date d2) {
		if ((d2.getTime() - d1.getTime()) > 600000l) {
			return true;
		}
		return false;
	}

	/**
	 * 闁兼儳鍢茶ぐ鍥蓟閹邦兙浜柡鍕靛灡濡诧箓寮甸悢宄版
	 * 
	 * @param d
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getTheDay(Date d) {
		return "闁哄啨鍎扮粩瀛樼鐏炶偐鐟忛柛銉︾◥缁ㄦ煡宕楅敓锟�".charAt(d.getDay()) + "";
	}

	/**
	 * 闁兼儳鍢茶ぐ鍥ь嚗濡や胶鍘欐鐐差嚟濞堟垹绮锟介崵鎴﹀川閵娧勭暠閻犙冨槻椤劙寮妷锔藉焸闁告粌鐬肩划銊╁级閻斿憡锛夐柡鍫嫹
	 * 
	 * @param year
	 *            妤犵偟绻濋崬锟�
	 * @param week
	 *            缂佹鍓欓崵鎴﹀川閿燂拷
	 * @return String 闁轰焦澹嗙划宥夋晬閿燂拷 [0] 閻犙冨槻椤劙寮妷锔藉焸 [1] 缂備焦鎸诲顐﹀籍閵夛附鍩�
	 */
	public static String[] weekDate(int year, int week) {
		if (week < 1 || week > 52)
			return null;
		String s[] = new String[2];
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.WEEK_OF_YEAR, week);
		c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		s[0] = formatYMD(c.getTime());
		c.add(Calendar.DATE, 6);
		s[1] = formatYMD(c.getTime());
		return s;

	}

	/**
	 * 闁兼儳鍢茶ぐ鍥ь嚗濡や胶鍘欐鐐差嚟濞堟垹绮锟介崵鎴﹀嫉閸垺鐣遍悹褍鍢查～鎰板籍閵夛附鍩傞柛婊冪灱缁劑寮堕悢鍛婏級闁哄牞鎷�
	 * 
	 * @param year
	 *            妤犵偟绻濋崬锟�
	 * @param month
	 *            缂佹鍓欓崵鎴﹀嫉閿燂拷
	 * @return String 闁轰焦澹嗙划宥夋晬閿燂拷 [0] 閻犙冨槻椤劙寮妷锔藉焸 [1] 缂備焦鎸诲顐﹀籍閵夛附鍩�
	 */
	public static String[] monthDate(int year, int month) {
		if (month < 1 || month > 12)
			return null;
		String s[] = new String[2];
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		s[0] = formatYMD(c.getTime());
		c.add(Calendar.MONTH, 1);
		c.add(Calendar.DATE, -1);
		s[1] = formatYMD(c.getTime());
		return s;

	}
	/**
	 * 闁兼儳鍢茶ぐ鍥ь嚗濡や胶鍘欐鐐差嚟濞堟垹绮锟介崵鎴狅拷娑崇到鐎规娊鎯冮崟顔藉闯濠殿喖顑嗗Λ鈺呭嫉閻旈攱瀚茬紓浣规尰濞碱偊寮妷锔藉焸
	 * 
	 * @param year
	 *            妤犵偟绻濋崬锟�
	 * @param season
	 *            缂佹鍓欓崵鎴狅拷娑虫嫹
	 * @return String 闁轰焦澹嗙划宥夋晬閿燂拷 [0] 閻犙冨槻椤劙寮妷锔藉焸 [1] 缂備焦鎸诲顐﹀籍閵夛附鍩�
	 */
	public static String[] seasonDate(int year, int season) {
		if (season < 1 || season > 4)
			return null;
		String y = String.valueOf(year);
		String[] s = new String[2];
		switch (season) {
		case 1:
			s[0] = y + "-01-01";
			s[1] = y + "-03-31";
			break;
		case 2:
			s[0] = y + "-04-01";
			s[1] = y + "-06-30";
			break;
		case 3:
			s[0] = y + "-07-01";
			s[1] = y + "-09-30";
			break;
		case 4:
			s[0] = y + "-10-01";
			s[1] = y + "-12-31";
			break;
		}
		return s;

	}

	/**
	 * 闁兼儳鍢茶ぐ鍥蓟閹邦剙瀣�闁哄本鍔栧﹢锟介柡鍫濐槸椤﹁法浜搁幋婵勪函 濠碘槄鎷�:20090225 閺夆晜鏌ㄥú锟�28
	 * 
	 * @param strDate
	 *            闁哄本鍔曢妵锟�
	 * @return
	 */
	@SuppressWarnings( { "static-access", "deprecation" })
	public static int getDaysOfMonth(String strDate) {
		int day = 0;
		Calendar cal = Calendar.getInstance();
		// 闁哄秶鍘х槐锟犲礌閺嶃劍锛夐柡鍫嫹
		SimpleDateFormat dformat = new SimpleDateFormat("yyyymmdd");
		try {
			Date date = dformat.parse(strDate);
			cal.setTime(date);
			// 闁革负鍔岀紞瀣礈瀹ュ棙绠戝ù鐘冲灊缁楀倿宕濋悩杈╊伇,闁汇垹褰夌花鐞侫VA缂佸甯傾N濞戞搫鎷�0,闁圭鎷峰ù鐘劥缁绘牠鏌岀仦钘夘潱2
			cal.add(cal.MONTH, 2);
			// 閻犱礁澧介悿鍡涘籍閵夛附鍩傚☉鎿勬嫹1闁告瑱鎷�
			cal.set(cal.DATE, 1);
			// 闁告碍鍨垫晶鐘绘焻閿熻姤绋夐敓鑺ュ緞閿燂拷
			cal.add(cal.DAY_OF_MONTH, -1);
			date = cal.getTime();
			// 鐎电増顨呴崺宀冦亹閹惧啿顤呴柡鍐挎嫹,闁告娅曞Σ鎼佸嫉椤掍焦绠戦柣銊ュ閵囧寮敓锟�
			day = date.getDate();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return day;
	}

	@SuppressWarnings("deprecation")
	public static int getDateOfMonth(Date date) {
		return date.getDate();
	}

	/**
	 * 闁兼儳鍢茶ぐ鍥亹閹惧啿顤呴柡鍐﹀劜濠�锟犳儍閸曨亞鐟撳☉鎿冧簼濠�锟介柣銊ュ鐎氥垽鐛幓鎺嬩函闁告艾娴峰▓鎴﹀籍閵夛附鍩�
	 * 
	 * @param days
	 * @return
	 */
	public static String getDateInNextMonthOfNextDays(int days) {
		return formatYMD(getNextDayCurrDay(StrToDate(getNextMonthFirst()), days));
	}

	/**
	 * 鐎电増顨呴崺灞剧鐏炲ジ鍤嬮柡鍐﹀劜濠�锟犳⒒鐎靛憡鐣遍梻鍌涙尦濞堁勫緞閳哄倹娈�
	 */
	public static String getTwoDay(String sj1, String sj2) {
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		long day = 0;
		try {
			java.util.Date date = myFormatter.parse(sj1);
			java.util.Date mydate = myFormatter.parse(sj2);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		} catch (Exception e) {
			return "";
		}
		return day + "";
	}

	/**
	 * 濞戞挶鍊撻柌婊堝籍閸洘锛熷☉鏂款儔濡潡鎯冮崟顐犱函闁轰緤鎷�
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getDays(String date1, String date2) {
		if (date1 == null || date1.equals(""))
			return 0;
		if (date2 == null || date2.equals(""))
			return 0;
		// 閺夌儐鍓氬畷鍙夌▔閻戞鍨奸柛鎴濇濡炲倿姊婚敓锟�
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date date = null;
		java.util.Date mydate = null;
		try {
			date = myFormatter.parse(date1);
			mydate = myFormatter.parse(date2);
		} catch (Exception e) {
		}
		long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		return day;
	}

	// 閻犱緤绱曢悾鏄忋亹閹惧瓨绠戦柡鍫嫹闁告艾绨肩粩瀛樺緞閿燂拷,閺夆晜鏌ㄥú鏍拷娑欘殘椤戜焦绋夐敓锟�
	public static String getDefaultDay() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 閻犱礁褰炵拹鐔汇亹閹惧啿顤呴柡鍫濈墢濞堬拷1闁告瑱鎷�
		lastDate.add(Calendar.MONTH, 1);// 闁告梻濮崇粩瀛樼▔椤忓懏绠戦柨娑樿嫰瑜板绋夋潪鎵憮闁哄牆鐗忓▓锟�1闁告瑱鎷�
		lastDate.add(Calendar.DATE, -1);// 闁告垵绻愰獮鎾寸▔閿熻姤寰勯埡瀣闁告瑦眉鐠愮喕銇愰幘瀛樼畱闁哄牞鎷烽柛姘凹缁斿瓨寰勯敓锟�

		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 濞戞挸锕ュ﹢锟界紒妤婂厸缁斿瓨寰勯敓锟�
	public static String getPreviousMonthFirst() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 閻犱礁褰炵拹鐔汇亹閹惧啿顤呴柡鍫濈墢濞堬拷1闁告瑱鎷�
		lastDate.add(Calendar.MONTH, -1);// 闁告垵绻嬬粩瀛樼▔椤忓懏绠戦柨娑樿嫰瑜板绋夋潪鎵憮闁哄牆鐗忓▓锟�1闁告瑱鎷�
		// lastDate.add(Calendar.DATE,-1);//闁告垵绻愰獮鎾寸▔閿熻姤寰勯埡瀣闁告瑦眉鐠愮喕銇愰幘瀛樼畱闁哄牞鎷烽柛姘凹缁斿瓨寰勯敓锟�

		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 闁兼儳鍢茶ぐ鍥亹閹惧瓨绠戠紒妤婂厸缁斿瓨寰勯敓锟�
	public static String getFirstDayOfMonth() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 閻犱礁褰炵拹鐔汇亹閹惧啿顤呴柡鍫濈墢濞堬拷1闁告瑱鎷�
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 闁兼儳鍢茶ぐ鍥亹閹剧偨浜柡鍐ㄧ埣濡拷
	public static String getNowTime(String dateformat) {
		Date now = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);// 闁告瑯鍨禍鎺楀棘闁稓鈹掗柛锕傤暒閹便劑寮ㄧ憴鍕級闁哄牏鍠愰悧绋款嚕閿燂拷
		String hehe = dateFormat.format(now);
		return hehe;
	}

	// 闁兼儳鍢茬欢杈亹閹惧啿顤呴柡鍐﹀劜濠�鈩冪▔鎼淬垺鎷遍柛娑栧妽濡晠鎯勭粙鎸庘枙闁汇劌瀚妵澶愬极閿燂拷
	private static int getMondayPlus() {
		Calendar cd = Calendar.getInstance();
		// 闁兼儳鍢茬欢杈ㄧ婵犲倶浜柡鍕靛灟缁旀挳宕ㄩ妸褎鐣辩紒妤婂墮閸ゆ垶寰勯埡瀣闁哄嫮鍠愬﹢锟犲籍閵夛附笑缂佹鍏涚粩瀛樺緞閳哄绀夐柡鍕枑濠�鈩冪鐏炵偓笑缂佹鍏涚花鈺傚緞閿燂拷......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 闁搞儳濮崇拹鐔煎箰婢跺鍘柛銉ф櫕閵囨粓骞忓锟界粩瀛樻媴濠婏拷鐠愮喓绮璺伇濠㈠灈鏅滄晶宥嗙閵夈劎绠归梺鎻掕嫰閸ｏ拷1
		if (dayOfWeek == 1) {
			return 0;
		} else {
			return 1 - dayOfWeek;
		}
	}

	// 闁兼儳鍢茬欢杈ㄧ▔鐎ｎ亝鍣柡鍕枑濠�锟犲籍閵壯勭暠闁哄啨鍎插﹢锟�
	public static String getNextSunday() {

		int mondayPlus = getMondayPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 + 6);
		Date monday = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preMonday = df.format(monday);
		return preMonday;
	}

	// 闁兼儳鍢茶ぐ鍥╃磼濞嗗繒鏆伴柡鍫濈墔閸炪倝鎯冮崟顓у剳濞戞搫鎷峰璁规嫹

	// 闁兼儳鍢茶ぐ鍥╃磼濞嗗繒鏆伴柡鍫濈墔閸炪倝鎯冮崟顒佷粯闁告艾绨肩粩瀛樺緞閿燂拷
	public static String getMonthEnd(String strdate) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
		// String str = "2012-05-02";
		Date date = df.parse(strdate);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		return df2.format(calendar.getTime());
	}

	// 闁兼儳鍢茬欢杈ㄧ▔婵犲啯绠戦柡鍫嫹闁告艾绨肩粩瀛樺緞閳哄啯鐣遍柡鍐﹀劜濠�锟�
	public static String getPreviousMonthEnd() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, -1);// 闁告垵绻嬬粩瀛樼▔椤忓懏绠�
		lastDate.set(Calendar.DATE, 1);// 闁硅泛锕ュΛ鈺呭嫉閻旀椿鍟庣紓鍐惧枙鐠愮喕銇愰幘瀛樼畱缂佹鍏涚粩瀛樺緞閿燂拷
		lastDate.roll(Calendar.DATE, -1);// 闁哄啨鍎插﹢锟犲炊閻愬娉婂☉鎿勬嫹濠㈠灈鏅槐婵囩▕閻旈攱鐨戦柡鍕靛灡濠�浼村嫉閸喐浠橀柛姘凹缁斿瓨寰勯敓锟�
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 闁兼儳鍢茬欢杈ㄧ▔鐎ｂ晠鍤嬮柡鍫濈墢椤戝洦绋夐敓鑺ュ緞閳哄啯鐣遍柡鍐﹀劜濠�锟�
	public static String getNextMonthFirst() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, 1);// 闁告垵绻嬬粩瀛樼▔椤忓懏绠�
		lastDate.set(Calendar.DATE, 1);// 闁硅泛锕ュΛ鈺呭嫉閻旀椿鍟庣紓鍐惧枙鐠愮喕銇愰幘瀛樼畱缂佹鍏涚粩瀛樺緞閿燂拷
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 闁兼儳鍢茬欢杈ㄧ▔鐎ｂ晠鍤嬮柡鍫濈墛濞撳爼宕ユ惔婵堫伇濠㈠灈鏅濆▓鎴﹀籍閵夛附鍩�
	public static String getNextMonthEnd() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, 1);// 闁告梻濮崇粩瀛樼▔椤忓懏绠�
		lastDate.set(Calendar.DATE, 1);// 闁硅泛锕ュΛ鈺呭嫉閻旀椿鍟庣紓鍐惧枙鐠愮喕銇愰幘瀛樼畱缂佹鍏涚粩瀛樺緞閿燂拷
		lastDate.roll(Calendar.DATE, -1);// 闁哄啨鍎插﹢锟犲炊閻愬娉婂☉鎿勬嫹濠㈠灈鏅槐婵囩▕閻旈攱鐨戦柡鍕靛灡濠�浼村嫉閸喐浠橀柛姘凹缁斿瓨寰勯敓锟�
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 闁兼儳鍢茬欢閬嶅及鎼粹�冲瑎闁哄牞鎷烽柛姘凹缁斿瓨寰勯埡鍐╃暠闁哄啨鍎插﹢锟�
	public static String getNextYearEnd() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.YEAR, 1);// 闁告梻濮崇粩瀛樼▔椤忓嫬瀣�
		lastDate.set(Calendar.DAY_OF_YEAR, 1);
		lastDate.roll(Calendar.DAY_OF_YEAR, -1);
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 闁兼儳鍢茬欢閬嶅及鎼粹�冲瑎缂佹鍏涚粩瀛樺緞閳哄啯鐣遍柡鍐﹀劜濠�锟�
	public static String getNextYearFirst() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.YEAR, 1);// 闁告梻濮崇粩瀛樼▔椤忓嫬瀣�
		lastDate.set(Calendar.DAY_OF_YEAR, 1);
		str = sdf.format(lastDate.getTime());
		return str;

	}

	// 闁兼儳鍢茬欢閬嶅嫉椤掞拷閸曢箖寮垫径濠庢▼閻忓繑鍨甸妵锟�
	public static int getMaxYear() {
		Calendar cd = Calendar.getInstance();
		cd.set(Calendar.DAY_OF_YEAR, 1);// 闁硅泛锕ュΛ鈺呭嫉閻旀椿鍟庡☉鎾虫惈缂嶅鐛�靛壊鍎戝☉鎿勬嫹濠㈣鎷�
		cd.roll(Calendar.DAY_OF_YEAR, -1);// 闁硅泛锕ュΛ鈺呭嫉閻旈攱绀�婵犲﹥鐭粩瀛樺緞閳瑰尅鎷烽敓锟�
		int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
		return MaxYear;
	}

	private static int getYearPlus() {
		Calendar cd = Calendar.getInstance();
		int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);// 闁兼儳鍢茬欢杈亹閹剧偨浜柡鍕靛灟缁旀挳鐛紙鐘哄幀闁汇劌瀚鍥礄閻樺眰浜�
		cd.set(Calendar.DAY_OF_YEAR, 1);// 闁硅泛锕ュΛ鈺呭嫉閻旀椿鍟庡☉鎾虫惈缂嶅鐛�靛壊鍎戝☉鎿勬嫹濠㈣鎷�
		cd.roll(Calendar.DAY_OF_YEAR, -1);// 闁硅泛锕ュΛ鈺呭嫉閻旈攱绀�婵犲﹥鐭粩瀛樺緞閳瑰尅鎷烽敓锟�
		int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
		if (yearOfNumber == 1) {
			return -MaxYear;
		} else {
			return 1 - yearOfNumber;
		}
	}

	// 闁兼儳鍢茬欢閬嶅嫉椤掞拷閸曞墽绮璺伇濠㈠灈鏅濆▓鎴﹀籍閵夛附鍩�
	public static String getCurrentYearFirst() {
		int yearPlus = getYearPlus();
		GregorianCalendar currentDate = new GregorianCalendar();
		currentDate.add(GregorianCalendar.DATE, yearPlus);
		Date yearDay = currentDate.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String preYearDay = df.format(yearDay);
		return preYearDay;
	}

	// 闁兼儳鍢茬欢閬嶅嫉椤掞拷閸曢箖寮甸敓浠嬪触鎼存繄顏卞鍨涙櫇濞堟垿寮妷锔藉焸 *
	public static String getCurrentYearEnd() {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 闁告瑯鍨禍鎺楀棘闁稓鈹掗柛锕傤暒閹便劑寮ㄧ憴鍕級闁哄牏鍠愰悧绋款嚕閿燂拷
		String years = dateFormat.format(date);
		return years + "-12-31";
	}

	// 闁兼儳鍢茬欢杈ㄧ▔婵犲倸瀣�缂佹鍏涚粩瀛樺緞閳哄啯鐣遍柡鍐﹀劜濠�锟� *
	public static String getPreviousYearFirst() {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 闁告瑯鍨禍鎺楀棘闁稓鈹掗柛锕傤暒閹便劑寮ㄧ憴鍕級闁哄牏鍠愰悧绋款嚕閿燂拷
		String years = dateFormat.format(date);
		int years_value = Integer.parseInt(years);
		years_value--;
		return years_value + "-1-1";
	}

	// 闁兼儳鍢茬欢閬嶅嫉椤掞拷椤掓粍鎯旈敓锟�
	public static String getThisSeasonTime(int month) {
		int array[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
		int season = 1;
		if (month >= 1 && month <= 3) {
			season = 1;
		}
		if (month >= 4 && month <= 6) {
			season = 2;
		}
		if (month >= 7 && month <= 9) {
			season = 3;
		}
		if (month >= 10 && month <= 12) {
			season = 4;
		}
		int start_month = array[season - 1][0];
		int end_month = array[season - 1][2];

		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 闁告瑯鍨禍鎺楀棘闁稓鈹掗柛锕傤暒閹便劑寮ㄧ憴鍕級闁哄牏鍠愰悧绋款嚕閿燂拷
		String years = dateFormat.format(date);
		int years_value = Integer.parseInt(years);

		int start_days = 1;// years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);
		int end_days = getLastDayOfMonth(years_value, end_month);
		String seasonDate = years_value + "-" + start_month + "-" + start_days
				+ ";" + years_value + "-" + end_month + "-" + end_days;
		return seasonDate;

	}

	/**
	 * 闁兼儳鍢茶ぐ鍥蓟閹邦剙瀣�闁哄本鍔栧﹢锟介柣銊ュ濞撳爼宕ユ惔婵堫伇濠㈣鎷�
	 * 
	 * @param year
	 *            妤犵儑鎷�
	 * @param month
	 *            闁哄牞鎷�
	 * @return 闁哄牞鎷烽柛姘凹缁斿瓨寰勯敓锟�
	 */
	private static int getLastDayOfMonth(int year, int month) {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12) {
			return 31;
		}
		if (month == 4 || month == 6 || month == 9 || month == 11) {
			return 30;
		}
		if (month == 2) {
			if (isLeapYear(year)) {
				return 29;
			} else {
				return 28;
			}
		}
		return 0;
	}

	/**
	 * 闁哄嫷鍨伴幆渚�姊婚弶鍨瑎
	 * 
	 * @param year
	 *            妤犵儑鎷�
	 * @return
	 */
	public static boolean isLeapYear(int year) {
		return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
	}

	/**
	 * 闁哄嫷鍨伴幆渚�寮伴姘�卞☉鎿勬嫹濠㈠灈鏅濆▓鎴﹀籍閸洘锛熼柛鎺嬪�栭弻鍥晬鐏炵厧鐦滈悷鏇氱劍濡茬顫㈤敐鍜佸殸闁哄牆顦花娲籍閸洘锛熼悽顖ら檮濡炲倿宕氶崱娆樻健闁挎稑鏈﹢浣圭濞戞瑦顦ч梻鍌滅節缁楀鏁敂鑺ヮ槯闁告帒妫涢～锟�
	 * 
	 * @author XuGuo
	 * @since 2009-04-13
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean isTheSameDay(Date d1, Date d2) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
				&& (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
				&& (c1.get(Calendar.DAY_OF_MONTH) == c2
						.get(Calendar.DAY_OF_MONTH));
	}

	/**
	 * 闁哄嫷鍨伴幆渚�寮伴姘�卞☉鎿勬嫹濠㈠灈鏅濆▓鎴﹀籍閸洘锛熼柛鎺嬪�栭弻鍥晬鐏炵厧鐦滈悷鏇氱劍濡茬顫㈤敐鍜佸殸闁哄牆顦花娲籍閸洘锛熼悽顖ら檮濡炲倿宕氶崱娆樻健闁挎稑鏈﹢浣圭濞戞瑦顦ч梻鍌滅節缁楀鏁敂鑺ヮ槯闁告帒妫涢～锟�
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean isTheSameMonth(Date d1, Date d2) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
				&& (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH));
	}

	/**
	 * 濞戞挶鍊撻柌婊堝籍閸洘锛熷☉鏂款儔濡潡鎯冮崟顓ф健闁轰緤鎷�
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long getSeconds(String date1, String date2) {
		if (date1 == null || date1.equals(""))
			return 0;
		if (date2 == null || date2.equals(""))
			return 0;
		// 閺夌儐鍓氬畷鍙夌▔閻戞鍨奸柛鎴濇濡炲倿姊婚敓锟�
		SimpleDateFormat myFormatter = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		java.util.Date date = null;
		java.util.Date mydate = null;
		try {
			date = myFormatter.parse(date1);
			mydate = myFormatter.parse(date2);
		} catch (Exception e) {
		}
		long day = (date.getTime() - mydate.getTime()) / 1000;
		return day;
	}
}